package com.hnevc.androidui10;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //获取文件data。json的输入流
        InputStream in = this.getResources().openRawResource(R.raw.data);
        try {
            //定义一个长度为:文件字节长度的字节数组bytes
            byte[] bytes = new byte[in.available()];
            in.read(bytes);//从输入流中读取字节,
            //将字节数组转换为字符串
            String temp = new String(bytes);


            //解析字符串到JSONobject
            JSONObject jsonObj = new JSONObject(temp);
            String author = jsonObj.getString("author");
            JSONArray array = jsonObj.getJSONArray("publishers");
            String publishers1 = (String) array.get(0);
            Toast.makeText(this, author+publishers1, Toast.LENGTH_SHORT).show();

        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }


    }
}
